package 题目集.前缀和.二维前缀和;

import java.util.Scanner;

//https://www.acwing.com/problem/content/101/
public class 激光炸弹 {
    static int n;
    static int r;
    static int[][] pre=new int[5002][5002];

    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        n=sc.nextInt();
        r=sc.nextInt();
        for (int i = 0; i < n; i++) {
            pre[sc.nextInt()+1][sc.nextInt()+1]+=sc.nextInt();
        }
        for (int i = 1; i < pre.length; i++) {
            for (int j = 1; j < pre.length; j++) {
                pre[i][j]+=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1];
            }
        }
        if (r>=5000){
            System.out.println(pre[5001][5001]);
            return;
        }
        int res=0;
        for (int i = r; i < pre.length; i++) {
            for (int j = r; j < pre.length; j++) {
                int area=pre[i][j];
                if (i>=r){
                    area-=pre[i-r][j];  //不包含左边界，所以不额外减一
                }
                if (j>=r){
                    area-=pre[i][j-r];  //不包含下边界，所以不额外减一
                }
                if (i>=r&&j>=r){
                    area+=pre[i-r][j-r];
                }
                res=Math.max(res,area);
            }
        }
        System.out.println(res);
    }
}
